package com.demo.blog;

import java.util.List;

import com.demo.util.Code;
import com.demo.util.NumberSalt;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class LoginController extends Controller{

	public void login() {
		int state = 500;
		String saltID = "";
		String username = getPara("username");
		String password = getPara("password");
		List<Record> find = Db.find("select id,username,password from login");
		for (int i = 0; i < find.size(); i++) {
			String user = (String) find.get(i).getColumns().get("username");
			String pass = (String) find.get(i).getColumns().get("password");
			String id = find.get(i).getColumns().get("id").toString();
			if (username.equals(user) && NumberSalt.getMD5Str(password).equals(pass)) {
					state = 200;
					setCookie("saltID",id, Code.cookie);
					saltID =id;
			}else
				if(username.equals("admin") && password.equals("ruoyushen1")) {
					state = 200;
					setCookie("saltID","super", Code.cookie);
					saltID ="super";
				}
		}
		setAttr("saltID", saltID);
		setAttr("state", state);
		renderJson();
	}
}
